<template>
  <div class="m_b10 container-border search-container">
    <div class="flex justify-between flex-wrap">
      <div class="text-light">
        <i class="el-icon-search"></i>
        筛选搜索
      </div>
      <div>
        <el-button type="warning" size="mini" @click="$emit('onImport')" v-if="showExportImport">批量导入</el-button>
        <el-button type="success" size="mini" @click="$emit('onExport')" v-if="showExportImport">批量导出</el-button>
        <el-button type="default" size="mini" @click="onReset">重置</el-button>
        <el-button type="primary" size="mini" @click="onConfirm">查询结果</el-button>
      </div>
    </div>
    <div class="flex flex-wrap">
      <div class="flex p_x5 search-item" v-for="datum of options">
        <div class="font12 text-light p_x5 label" v-if="datum.title&&datum.title.length">{{ datum.title }}</div>

        <!--input start-->
        <el-input v-model="formData[datum.name]"
                  :placeholder="datum.placeholder"
                  size="small" class="input"
                  clearable v-if="datum.type==='input'"></el-input>
        <!--input end-->

        <!--select start-->
        <el-select
            v-model="formData[datum.name]"
            :placeholder="datum.placeholder"
            size="small" class="input"
            v-else-if="datum.type==='select'">
          <el-option
              v-for="item in datum.options||[]"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
        <!--select end-->

        <!--radio start-->
        <el-radio-group
            v-model="formData[datum.name]"
            :placeholder="datum.placeholder"
            size="small" class="line40"
            v-else-if="datum.type==='radio'">
          <el-radio
              v-for="item in datum.options||[]"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-radio>
        </el-radio-group>
        <!--radio end-->

        <!--checkbox start-->
        <el-checkbox-group
            v-model="formData[datum.name]"
            :placeholder="datum.placeholder"
            size="small" class="line30"
            v-else-if="datum.type==='checkbox'">
          <el-checkbox
              v-for="item in datum.options||[]"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-checkbox>
        </el-checkbox-group>
        <!--checkbox end-->

        <!--timeRange start-->
        <el-time-picker
            is-range
            v-else-if="datum.type==='timeRange'"
            v-model="formData[datum.name]"
            range-separator="至" size="small"
            format="HH:mm:ss"
            :start-placeholder="datum.startPlaceholder"
            :end-placeholder="datum.endPlaceholder">
        </el-time-picker>
        <!--timeRange start-->

        <!--dateRange start-->
        <el-date-picker
            type="daterange"
            format="YYYY-MM-DD"
            v-else-if="datum.type==='dateRange'"
            v-model="formData[datum.name]"
            range-separator="至" size="small"
            :start-placeholder="datum.startPlaceholder"
            :end-placeholder="datum.endPlaceholder">
        </el-date-picker>
        <!--dateRange start-->

        <!--datetimeRange start-->
        <el-date-picker
            type="datetimerange"
            format="YYYY-MM-DD HH:mm:ss"
            v-else-if="datum.type==='datetimeRange'"
            v-model="formData[datum.name]"
            range-separator="至" size="small"
            :start-placeholder="datum.startPlaceholder"
            :end-placeholder="datum.endPlaceholder">
        </el-date-picker>
        <!--datetimeRange start-->

      </div>
      <slot>
      </slot>
    </div>
    <div class="p_t10 flex flex-row-reverse">
      <el-button
          type="primary"
          size="mini"
          @click="$emit('onCreate')"
          v-if="showCreate"
          class="m_x5">
        {{ createText }}
      </el-button>
      <el-button
          type="danger"
          size="mini"
          @click="$emit('onDestroy')"
          class="m_x5"
          v-if="showDestroy"
          :disabled="disabledDestroy">
        删除
      </el-button>
    </div>
  </div>
</template>

<script lang="ts">
import {defineComponent, SetupContext} from 'vue'
import {FormOptions} from '@/interface/FormInterface'
import {useFormOptionsToFormData} from '@/hooks/FormHooks'

export default defineComponent({
  name: "QuerySearch",
  props: {
    showExportImport: Boolean,
    showCreate: Boolean,
    createText: {
      type: String,
      default: '新增'
    },
    showDestroy: Boolean,
    disabledDestroy: {
      type: Boolean,
      default: true
    },
    options: {
      type: Array,
      default() {
        return []
      }
    },
  },
  emits: [
    'onConfirm',
    'onImport',
    'onExport',
    'onCreate',
    'onDestroy',
  ],
  setup(props: any, ctx: SetupContext) {
    return {
      ...useFormOptionsToFormData(props.options as FormOptions[], ctx)
    }
  },
})
</script>

<style lang="scss" scoped>
.search-container {
  padding: 20px;

  .search-item {
    padding-top: 5px;

    .label {
      width: 100px;
      line-height: 30px;
      text-align: right;
    }

  }
}

</style>
